import datetime
import random

from django.db import models


# 用户表
class User(models.Model):
    name = models.CharField(max_length=30, verbose_name='姓名')
    sex = models.CharField(max_length=2,verbose_name='性别')
    info = models.CharField(max_length=200, verbose_name='描述')
    auth = models.IntegerField(choices=((1, '用户'), (2, '商家管理员')), default=1, verbose_name='权限管理')
    # order = models.ForeignKey('models.Order',on_delete=models.CASCADE,verbose_name='订单商品')
    mobile = models.CharField(max_length=11, verbose_name="手机号")
    password = models.CharField(max_length=100,verbose_name='密码')
    ident = models.CharField(max_length=18, default=None, verbose_name="身份证号")

    class Meta:
        verbose_name = '用户'
        verbose_name_plural = verbose_name


# 地址
class Address(models.Model):
    addname = models.CharField(max_length=100,verbose_name='地址')
    longi = models.CharField(max_length=100,verbose_name='经纬度')
    set_def = models.BooleanField('是否设为默认地址',default=False)
    mobile = models.CharField('手机号', max_length=11)
    user = models.ForeignKey(User,on_delete=models.CASCADE,verbose_name='用户',related_name='user_aid')

    class Meta:
        verbose_name = '地址'
        verbose_name_plural = verbose_name


# 商家表
class Merchant(models.Model):
    merch_name = models.CharField(max_length=30,verbose_name='商家名称')
    logo = models.CharField(max_length=300,verbose_name='logo')
    many = models.IntegerField(verbose_name='起送费')
    info = models.CharField(max_length=100,verbose_name='描述')
    user = models.ForeignKey(User,on_delete=models.CASCADE,verbose_name='用户',related_name='user_bid')
    address = models.ForeignKey(Address,on_delete=models.CASCADE,verbose_name='配送地址',related_name='user_cid')

    class Meta:
        verbose_name = '商家表'
        verbose_name_plural = verbose_name


# 菜品表
class Dishes(models.Model):
    id = models.IntegerField(primary_key=True, auto_created=True, verbose_name="菜品ID")
    package_name = models.CharField("套餐名", max_length=16, default="")
    name = models.CharField('菜名', max_length=16, default="")
    dishes_logo = models.CharField('菜品标志', max_length=300, default='')
    price = models.DecimalField('单价',decimal_places=2,max_digits=7)
    num = models.IntegerField(verbose_name='数量')
    describe = models.CharField(max_length=64, default='', verbose_name="描述")
    Introduction = models.CharField(max_length=64, default='', verbose_name="描述")
    merchants = models.ForeignKey(Merchant, on_delete=models.CASCADE,related_name='user_lid')

    class Meta:
        verbose_name = '菜品表'
        verbose_name_plural = verbose_name


# 订单
class Order(models.Model):
    number = models.IntegerField(primary_key=True,verbose_name='订单号',default=random.randint(100000,999999))
    create_time = models.DateTimeField(default=datetime.datetime.now(), verbose_name="订单时间")
    # status = models.CharField(max_length=20 ,verbose_name="订单状态")
    status = models.IntegerField(default=0,choices=((0,'待付款'),(1,'待派送'),(2,'已派送'),(3,'已完成')))
    remark = models.CharField(max_length=100, verbose_name="备注")
    addre = models.ForeignKey(Address,on_delete=models.CASCADE,verbose_name='地址',related_name='user_fid')
    mercha = models.ForeignKey(Merchant,on_delete=models.CASCADE,verbose_name='商家',related_name='user_gid')
    dishes = models.ForeignKey(Dishes,on_delete=models.CASCADE,verbose_name='菜品',related_name='user_dishes')
    user = models.ForeignKey(User,on_delete=models.CASCADE,verbose_name='用户',related_name='user_user')
    amount = models.DecimalField('总价格',decimal_places=2,max_digits=7)

    class Meta:
        verbose_name = 'order'
        verbose_name_plural = verbose_name


# 商品类别表
class Commodity(models.Model):
    comm_name = models.CharField(max_length=30,verbose_name='商品类别')
    mercha = models.ForeignKey(Merchant, on_delete=models.CASCADE, verbose_name='商家',related_name='user_pid')

    class Meta:
        verbose_name = '商品类别'
        verbose_name_plural = verbose_name





# 商品订单关系表
class Goods_order(models.Model):
    dishes = models.ForeignKey(Dishes, on_delete=models.CASCADE,related_name='dishes_oid')
    num = models.IntegerField('数量', default='')
    price = models.DecimalField('价格',decimal_places=2,max_digits=7)
    order = models.ForeignKey(Order, on_delete=models.CASCADE,related_name='user_tid')

    class Meta:
        verbose_name = '商品订单关系'
        verbose_name_plural = verbose_name


# 购物车
class Shoppcart(models.Model):
    dishes = models.ForeignKey(Dishes, on_delete=models.CASCADE, related_name='dishes_id')
    shop_num = models.IntegerField('数量', default='')
    total_price = models.IntegerField('总价', default='')
    user = models.ForeignKey(User, on_delete=models.CASCADE,related_name='user_qid')

    class Meta:
        verbose_name = '购物车'
        verbose_name_plural = verbose_name


# 套餐菜品关系表
class Mealdishes(models.Model):
    package = models.ForeignKey(Dishes, on_delete=models.CASCADE,related_name='user_ld')
    dishes = models.ForeignKey(Dishes, on_delete=models.CASCADE,related_name='user_nid')
    dishes_num = models.IntegerField('菜品数量', default='')
    price = models.DecimalField('价格',decimal_places=2,max_digits=7)
    classification = models.ForeignKey(Commodity,on_delete= models.CASCADE,related_name='classification')

    class Meta:
        verbose_name = '套餐菜品关系'
        verbose_name_plural = verbose_name


# 口味表
class Taste(models.Model):
    id = models.IntegerField(primary_key=True, auto_created=True, verbose_name="口味id")
    name = models.CharField('口味名', max_length=12, default='')

    class Meta:
        verbose_name = '口味'
        verbose_name_plural = verbose_name


# 口味菜品关系表
class Tastedishes(models.Model):
    taste = models.ForeignKey(Taste,on_delete= models.CASCADE,related_name='taste')
    dishes = models.ForeignKey(Dishes, on_delete=models.CASCADE,related_name='dishes_ids')
    specific = models.CharField('具体口味', max_length=20, default='')
    class Meta:
        verbose_name = '口味表'
        verbose_name_plural = verbose_name


